﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PwjCy.Lite.Common
{
    /// <summary>
    /// 类型帮助类
    /// </summary>
    public class TypeHelper
    {
        private static readonly Lazy<TypeHelper> lazy = new Lazy<TypeHelper>(() => new TypeHelper());
        private TypeHelper() { }
        private string _PrefixKey { get; set; }
        public string PrefixKey
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_PrefixKey)) { return _PrefixKey; }
                _PrefixKey = this.GetType().FullName;
                return _PrefixKey;
            }
        }
        public static TypeHelper Instance { get { return lazy.Value; } }
        /// <summary>
        /// 获取类型属性信息
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public PropertyInfo[] GetPropertyInfo(Type t)
        {
            if (null != HashCacheHelper.Instance.GetCache<PropertyInfo[]>(PrefixKey + t.FullName))
            {
                return HashCacheHelper.Instance.GetCache<PropertyInfo[]>(PrefixKey + t.FullName);
            }
            PropertyInfo[] PropertyInfoArray = t.GetProperties();
            HashCacheHelper.Instance.AddCache<PropertyInfo[]>(PropertyInfoArray, PrefixKey + t.FullName);
            return PropertyInfoArray;
        }
        /// <summary>
        /// 改变类型
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public object ChangeType(Type type, object value)
        {
            if (null == value && type.IsGenericType)//为空且为泛型
            {
                return Activator.CreateInstance(type);
            }
            if (null == value)
            {
                return null;
            }
            if (type == value.GetType())//类型相同，直接返回
            {
                return value;
            }
            if (type.IsEnum)//枚举类型
            {
                if (value is string) return Enum.Parse(type, value as string);
                return Enum.ToObject(type, value);//讲具有整数值的对象转换为枚举
            }
            if (typeof(bool) == type && typeof(int).IsInstanceOfType(value))//确定指定的对象为当前System.Type的实例
            {
                return 0 != int.Parse(value.ToString());
            }
            if (!type.IsInterface && type.IsGenericType)//不为接口且为泛型
            {
                return Activator.CreateInstance(type, new object[] { ChangeType(type.GetGenericArguments()[0], value) });
            }
            if ((value is string) && (type == typeof(Guid)))
            {
                return new Guid(value as string);
            }
            if ((value is string) && (type == typeof(Version)))
            {
                return new Version(value as string);
            }
            if (!(value is IConvertible))
            {
                return value;
            }
            return Convert.ChangeType(value, type);
        }
    }
}
